import { HubConnectionBuilder, LogLevel } from '@microsoft/signalr'

export const createConsumer = (connected, received, accountId, pubsubToken, authToken) => {
  const connection = new HubConnectionBuilder()
    .withUrl(`http://localhost:44300/signalr-hubs/chat?type=contact`,
      {
        accessTokenFactory: () => authToken
      })
    // .withAutomaticReconnect([0, 2000, 10000, 30000])
    .configureLogging(LogLevel.Information)
    .build()

  connection.on('receiveMessage', received)
  connection.on('Connected', connected)
  // connection.on('ReceiveMessage', function (event, message) {
  //   console.log(event + ',' + message)
  // })
  connection.start()
  connection.onclose(async () => {
    console.log('onclose')
    await start()
  })
  async function start () {
    try {
      await connection.start()
      console.log('SignalR Connected.')
    } catch (err) {
      console.log(err)
      setTimeout(start, 5000)
    }
  }

  return connection
}

// const connection = new HubConnectionBuilder()
//   .withUrl('http://localhost:5000/question-hub')
//   .configureLogging(LogLevel.Information)
//   .build()

// connection.on('ReceiveMessage', function (message) {
//   console.log(message)
// })

// connection.start()
